package ro.dta.idbi.ui.sql;

import ro.dta.idbi.interfaces.sql.ISqlColumn;
import ro.dta.idbi.model.Utils;

import com.mxgraph.model.mxGeometry;

import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;

/**
 * Sql Column class used in the SQL diagram component
 * 
 * @author Tudor Dobrila
 * 
 */
public class SqlColumnCell extends AbstractSqlCell implements Cloneable, ISqlColumn {

	private static final long serialVersionUID = 8955045000946320032L;

	/**
	 * True if column is part of the PK constraint, false otherwise
	 */
	protected transient boolean primaryKey;

	/**
	 * True if column is nullable, false otherwise
	 */
	protected transient boolean nullable;

	/**
	 * Type of the column
	 */
	private transient String type;

	/**
	 * mxSqlColumnCell class constructor
	 * 
	 * @param schemaObj
	 *            AutoMed schema object of this column
	 * @param ident
	 *            ID of construct
	 * @param value
	 *            Value of cell
	 * @param xPos
	 *            X position of cell
	 * @param yPos
	 *            Y position of cell
	 * @param width
	 *            Width of cell
	 * @param height
	 *            Height of cell
	 * @param style
	 *            Style of the cell
	 */
	public SqlColumnCell(final Schema schema, final SchemaObject schemaObj, final String ident,
			final String value, final double xPos, final double yPos, final double width,
			final double height, final String style) {
		super(schema, schemaObj, ident, value, new mxGeometry(xPos, yPos, width, height), style);
		setVertex(true);
	}

	/**
	 * mxSqlColumnCell class constructor
	 * 
	 * @param schemaObj
	 *            AutoMed schema object of this column
	 * @param ident
	 *            ID of construct
	 * @param value
	 *            Value of cell
	 * @param geometry
	 *            Geometry of the column
	 * @param style
	 *            Style of the cell
	 */
	public SqlColumnCell(final Schema schema, final SchemaObject schemaObj, final String ident,
			final String value, final mxGeometry geometry, final String style) {
		super(schema, schemaObj, ident, value, geometry, style);
		setVertex(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.dta.idbi.sql.ui.ISqlColumn#setPK(boolean)
	 */
	@Override
	public void setPK(final boolean isPK) {
		this.primaryKey = isPK;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.dta.idbi.sql.ui.ISqlColumn#isPK()
	 */
	@Override
	public boolean isPK() {
		return primaryKey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.dta.idbi.sql.ui.ISqlColumn#setNullable(boolean)
	 */
	@Override
	public void setNullable(final boolean isNullable) {
		this.nullable = isNullable;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.dta.idbi.sql.ui.ISqlColumn#isNullable()
	 */
	@Override
	public boolean isNullable() {
		return nullable;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		final SqlColumnCell obj = (SqlColumnCell) super.clone();
		obj.primaryKey = primaryKey;
		obj.nullable = nullable;
		return obj;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof ISqlColumn) {
			ISqlColumn col = (ISqlColumn) obj;
			boolean isOk1 = true;
			if (col.getParent() != null && parent != null && parent.getValue() != null
					&& col.getParent().getValue() != null) {
				isOk1 = col.getParent().getValue().equals(parent.getValue());
			}
			boolean isOk2 = col.getValue().equals(getValue());
			return isOk1 && isOk2;
		}

		return false;
	}

	/**
	 * Set the type of the column
	 * 
	 * @param type
	 *            New type of the column
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return The type of the column
	 */
	public String getType() {
		return type;
	}

	/**
	 * Get the short definition of the column
	 * 
	 * @return Short definition in the form &lt;&lt;table,col&gt;&gt;
	 */
	@Override
	public String getShortDefinition() {
		StringBuilder builder = new StringBuilder("<<");
		if (parent != null) {
			builder.append(parent.getValue()).append(",");
		}
		builder.append(getName()).append(">>");
		return builder.toString();
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder("<<");
		if (parent != null) {
			builder.append(parent.getValue()).append(",");
		}
		builder.append(getValue()).append(",");
		builder.append(nullable ? "null" : "notnull").append(",");
		if (type != null) {
			builder.append(type).append(",");
		}
		builder.setLength(builder.length() - 1);
		builder.append(">>");
		return builder.toString();
	}

	@Override
	public String getName() {
		return getValue().toString().replace("?", "");
	}

	@Override
	public String getSchemeDefinition() {
		return Utils.genColRepresentation(getParent().getValue(), getValue(), isNullable() ? "null"
				: "notnull", type);
	}
}
